from sanic import Sanic
# from sanic_cors import CORS
from sanic_ext import Extend

from app.blueprints import configure_blueprints
from app.exception_handlers import CustomHandler, configure_exception_handlers
from app.listeners import configure_listeners
from app.middlewares import configure_middlewares
from app.signals import configure_signals

from settings import get_config

config = get_config()


def create_app() -> Sanic:
    # error_handler = CustomHandler()
    app: Sanic = Sanic(
        name=config.PROJECT_NAME,
        # error_handler=error_handler,
        config=config
    )
    configure_blueprints(app)   # 配置蓝图
    configure_listeners(app)    # 配置监听：redis...
    configure_middlewares(app)  # 配置中间件
    configure_signals(app)      # 配置信号

    configure_exception_handlers(app)  # 配置异常处理Handler

    Extend(app)                 # 扩展
    # CORS(app, automatic_options=True)
    return app
